package com.xiaoyu.lang;

import java.util.Map;

import com.xiaoyu.objpool.StringBufPool;
import com.xiaoyu.text.EasyTemplate;

public final class PStr {
	public static final PStr b() {
		PStr ret = new PStr();
		return ret;
	}

	public static final PStr b(final Object... objs) {
		return begin(objs);
	}

	public static final PStr begin() {
		PStr ret = new PStr();
		return ret;
	}

	public static final PStr begin(final Object... objs) {
		PStr ret = begin();
		ret.a(objs);
		return ret;
	}

	public static final PStr begin(final Object v) {
		String s = String.valueOf(v);
		return begin(s);
	}

	public static final PStr b(final Object v) {
		String s = String.valueOf(v);
		return begin(s);
	}

	public static final PStr begin(final String s) {
		PStr ret = begin();
		return ret.a(s);
	}

	public static final PStr b(final String s) {
		PStr ret = begin();
		return ret.a(s);
	}

	// ///////////////////////////////////////////////////
	public PStr() {
		sb = StringBufPool.borrowObject();
	}

	public final PStr clear() {
		sb.setLength(0);
		return this;
	}

	public final String end(final Object v) {
		String s = String.valueOf(v);
		return end(s);
	}

	public final String e(final Object v) {
		String s = String.valueOf(v);
		return end(s);
	}

	public final String end(final String s) {
		a(s);
		return end();
	}

	public final String e(final String s) {
		a(s);
		return end();
	}

	public final String end() {
		String ret = sb.toString();
		StringBufPool.returnObject(sb);
		sb = null;
		return ret;
	}

	public final String e() {
		String ret = sb.toString();
		StringBufPool.returnObject(sb);
		sb = null;
		return ret;
	}

	// ///////////////////////////////////////////////////
	private StringBuffer sb = null;

	public final PStr a(final boolean v) {
		sb.append(v);
		return this;
	}

	public final PStr a(final byte v) {
		sb.append(v);
		return this;
	}

	public final PStr a(final short v) {
		sb.append(v);
		return this;
	}

	public final PStr a(final int v) {
		sb.append(v);
		return this;
	}

	public final PStr a(final long v) {
		sb.append(v);
		return this;
	}

	public final PStr a(final float v) {
		sb.append(v);
		return this;
	}

	public final PStr a(final double v) {
		sb.append(v);
		return this;
	}

	public final PStr a(final String v) {
		sb.append(v);
		return this;
	}

	public final PStr a(final String str, final Map<String, Object> map)
			throws Exception {
		String v = EasyTemplate.make(str, map);
		sb.append(v);
		return this;
	}

	public final PStr a(final StringBuffer v) {
		sb.append(v);
		return this;
	}

	public final PStr a(final Object... objs) {
		for (Object obj : objs) {
			a(obj);
		}
		return this;
	}

	public final PStr a(final Object v) {
		sb.append(String.valueOf(v));
		return this;
	}

	public final PStr ap(final String fmt, final Object... args) {
		try {
			String s2 = StrEx.fmt(fmt, args);
			return a(s2);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return this;
	}

	public static final String str(final String fmt, final Object... args) {
		PStr ret = begin();
		ret.ap(fmt, args);
		return ret.end();
	}

	public static final String str(final Object... args) {
		StringBuffer sb = StringBufPool.borrowObject();
		try {
			for (Object o : args) {
				sb.append(o);
			}
			return sb.toString();
		} finally {
			StringBufPool.returnObject(sb);
		}
	}

	public final PStr delLeft(final int i) {
		sb.replace(0, i, "");
		return this;
	}

	public final PStr delRight(final int i) {
		// int len = sb.length();
		// sb.replace(len - i, len, "");
		StrEx.removeRight(sb, i);
		return this;
	}

	public static void main(String[] args) {
		String s = PStr.begin(123).a("dddd").a(true).delRight(4).end();
		System.out.println(s);
	}

	public int length() {
		return sb.length();
	}
}
